home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / Online / Term / Extras / Source / term-source.lha / FixPath.c < prev    next >
C/C++ Source or Header  |  1996-10-20  |  3KB  |  137 lines

  1. /*
  2. **    FixPath.c
  3. **
  4. **    Fix the current Process search patch list by faking a CLI
  5. **
  6. **    Copyright © 1990-1996 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. **
  9. **    :ts=4
  10. */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "Global.h"
  14. #endif
  15.  
  16.     /* This is how a linked list of directory search paths looks like. */
  17.  
  18. struct Path
  19. {
  20.     BPTR path_Next;    /* Pointer to next entry */
  21.     BPTR path_Lock;    /* The drawer in question; may be NULL */
  22. };
  23.  
  24.     /* ClonePath(BPTR StartPath):
  25.      *
  26.      *    Make a copy of the command search path attached to a
  27.      *    CLI process.
  28.      */
  29.  
  30. STATIC BPTR
  31. ClonePath(BPTR StartPath)
  32. {
  33.     struct Path *First,*Last,*List,*New;
  34.  
  35.     for(List = BADDR(StartPath), First = Last = NULL ; List ; List = BADDR(List->path_Next))
  36.     {
  37.         if(List->path_Lock)
  38.         {
  39.             if(New = AllocVec(sizeof(struct Path),MEMF_ANY))
  40.             {
  41.                 if(New->path_Lock = DupLock(List->path_Lock))
  42.                 {
  43.                     New->path_Next = NULL;
  44.  
  45.                     if(Last)
  46.                         Last->path_Next = MKBADDR(New);
  47.  
  48.                     if(!First)
  49.                         First = New;
  50.  
  51.                     Last = New;
  52.                 }
  53.                 else
  54.                 {
  55.                     FreeVec(New);
  56.                     break;
  57.                 }
  58.             }
  59.             else
  60.                 break;
  61.         }
  62.     }
  63.  
  64.     return(MKBADDR(First));
  65. }
  66.  
  67.     /* AttachCLI(struct WBStartup *Startup):
  68.      *
  69.      *    Attach a valid CLI structure to the current process. Requires a
  70.      *    Workbench startup message whose command search path it will
  71.      *    duplicate.
  72.      */
  73.  
  74. VOID
  75. AttachCLI(struct WBStartup *Startup)
  76. {
  77.     struct CommandLineInterface *DestCLI;
  78.  
  79.         /* Note: FreeDosObject can't free it, but the DOS */
  80.         /*       process termination code can. */
  81.  
  82.     if(DestCLI = AllocDosObject(DOS_CLI,NULL))
  83.     {
  84.         struct MsgPort *ReplyPort;
  85.         struct Process *Dest;
  86.  
  87.         DestCLI->cli_DefaultStack = 4096 / sizeof(ULONG);
  88.  
  89.         Dest = (struct Process *)FindTask(NULL);
  90.  
  91.         Dest->pr_CLI     = MKBADDR(DestCLI);
  92.         Dest->pr_Flags     |= PRF_FREECLI;            /* Mark for cleanup */
  93.  
  94.         Forbid();
  95.  
  96.         ReplyPort = Startup->sm_Message.mn_ReplyPort;
  97.  
  98.             /* Does the reply port data point somewhere sensible? */
  99.  
  100.         if(ReplyPort && (ReplyPort->mp_Flags & PF_ACTION) == PA_SIGNAL && TypeOfMem(ReplyPort->mp_SigTask))
  101.         {
  102.             struct Process *Father;
  103.  
  104.                 /* Get the address of the process that sent the startup message */
  105.  
  106.             Father = (struct Process *)ReplyPort->mp_SigTask;
  107.  
  108.                 /* Just to be on the safe side... */
  109.  
  110.             if(Father->pr_Task.tc_Node.ln_Type == NT_PROCESS)
  111.             {
  112.                 struct CommandLineInterface    *SourceCLI;
  113.  
  114.                     /* Is there a CLI attached? */
  115.  
  116.                 if(SourceCLI = BADDR(Father->pr_CLI))
  117.                 {
  118.                     STRPTR Prompt;
  119.  
  120.                         /* Clone the other CLI data. */
  121.  
  122.                     if(Prompt = (STRPTR)BADDR(SourceCLI->cli_Prompt))
  123.                         SetPrompt(&Prompt[1]);
  124.  
  125.                     if(SourceCLI->cli_DefaultStack > DestCLI->cli_DefaultStack)
  126.                         DestCLI->cli_DefaultStack = SourceCLI->cli_DefaultStack;
  127.  
  128.                     if(SourceCLI->cli_CommandDir)
  129.                         DestCLI->cli_CommandDir = ClonePath(SourceCLI->cli_CommandDir);
  130.                 }
  131.             }
  132.         }
  133.  
  134.         Permit();
  135.     }
  136. }
  137.